import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * @Author 12629
 * @Description：
 */
public class Test {


    public static void main(String[] args) {
        MySingleList mySingleList1 = new MySingleList();
        mySingleList1.addLast(12);
        mySingleList1.addLast(23);
        mySingleList1.addLast(34);
        mySingleList1.addLast(45);
        mySingleList1.addLast(56);
        mySingleList1.display();

        mySingleList1.reversePrintList();

    }

    public static void main3(String[] args) {
        List<Integer> list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);

        for(int x : list) {
            System.out.println(x);
        }

        System.out.println("=====");
        ListIterator<Integer> it =  list.listIterator();
        while (it.hasNext()) {
            System.out.print(it.next()+" ");
        }
        System.out.println();
        System.out.println("=====");
        ListIterator<Integer> it2 =  list.listIterator(list.size());
        while (it2.hasPrevious()) {
            System.out.print(it2.previous()+" ");
        }
        System.out.println();
    }




    public static void main2(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addLast(1);
        myLinkedList.addLast(1);
        myLinkedList.addLast(1);
        myLinkedList.addLast(1);
        ///System.out.println(myLinkedList.size());
        //System.out.println(myLinkedList.contains(10));
        myLinkedList.display();

        myLinkedList.removeAllKey(1);
        System.out.println("====");
        myLinkedList.display();

        /*myLinkedList.addIndex(2,99);
        myLinkedList.display();
*/
    }




    public static MySingleList.ListNode mergeTwoLists(MySingleList.ListNode headA,
                                                      MySingleList.ListNode headB) {

        MySingleList.ListNode newHead =
                new MySingleList.ListNode(-1);//不具备有效数据
        MySingleList.ListNode tmpHead = newHead;

        while(headA != null && headB != null) {
            if(headA.val > headB.val) {
                tmpHead.next = headB;
                headB = headB.next;
            }else {
                tmpHead.next = headA;
                headA = headA.next;
            }
            tmpHead = tmpHead.next;
        }

        if(headA != null) {
            tmpHead.next = headA;
        }

        if(headB != null) {
            tmpHead.next = headB;
        }

        return newHead.next;
    }


    public static MySingleList.ListNode getIntersectionNode(MySingleList.ListNode headA,
                                                            MySingleList.ListNode headB) {
        //1、分别求2个链表的长度
        int lenA = 0;
        int lenB = 0;

        MySingleList.ListNode pL = headA;//假设pL 所指向的链表 是最长的链表
        MySingleList.ListNode pS = headB;//假设pS 所指向的链表 是最短的链表
        while(pL != null) {
            lenA++;
            pL = pL.next;
        }

        while(pS != null) {
            lenB++;
            pS = pS.next;
        }

        pL = headA;
        pS = headB;

        //分别求得了 2个链表的长度
        int len = lenA - lenB;
        //修正指向 和 len的差值
        if(len < 0) {
            pL = headB;
            pS = headA;
            len = lenB - lenA;
        }
        //我们都能保证PL一定指向最长的链表  PS 一定指向最短的链表  len 一定是一个正数
        //2、让最长的链表 先走差值步
        while(len != 0) {
            pL = pL.next;
            len--;
        }

        //3、就是相遇的点
        while(pL != pS) {
            pL = pL.next;
            pS = pS.next;
        }
        return pL;
    }


    private static void createIntersect(MySingleList.ListNode headA,
                                        MySingleList.ListNode headB) {
        headB.next.next = headA.next.next;
    }

    public static void main31(String[] args) {
        MySingleList mySingleList1 = new MySingleList();
        mySingleList1.addLast(12);
        mySingleList1.addLast(23);
        mySingleList1.addLast(34);
        mySingleList1.addLast(45);
        mySingleList1.addLast(56);

        mySingleList1.display();



        mySingleList1.createLoop();

        System.out.println(mySingleList1.hasCycle());

        MySingleList.ListNode ret =
                mySingleList1.detectCycle();
        System.out.println(ret.val);
        //mySingleList1.display();




        MySingleList mySingleList2 = new MySingleList();
        mySingleList2.addLast(8);
        mySingleList2.addLast(15);
        mySingleList2.addLast(30);
        mySingleList2.addLast(40);
        mySingleList2.addLast(78);
        mySingleList2.display();

        /*createIntersect(mySingleList1.head,mySingleList2.head);//34

        MySingleList.ListNode ret =getIntersectionNode(mySingleList1.head,mySingleList2.head);
        System.out.println(ret.val);*/


       /* MySingleList.ListNode nH = mergeTwoLists(mySingleList1.head,mySingleList2.head);

        mySingleList1.display(nH);*/

    }


    public static void main1(String[] args) {
        MySingleList mySingleList = new MySingleList();
        //mySingleList.createList();
        mySingleList.addFirst(12);
        mySingleList.addFirst(23);
        mySingleList.addFirst(34);
        mySingleList.addFirst(45);
        mySingleList.addFirst(56);
        mySingleList.display();/**/
        System.out.println("=====");

        MySingleList.ListNode ret  =
                mySingleList.middleNode();

        System.out.println(ret.val);

        /*MySingleList.ListNode newHead =
                mySingleList.reverseList();

        mySingleList.display(newHead);*/


        //mySingleList.display();/**/


        //mySingleList.remove(34);
        //mySingleList.display();/**/
        //mySingleList.addLast(199);
        /*mySingleList.addIndex(0,199);
        mySingleList.display();
        mySingleList.addIndex(5,299);
        mySingleList.display();
        mySingleList.addIndex(3,9999);
        mySingleList.display();*/
        //System.out.println(mySingleList.size());
        //System.out.println(mySingleList.contains(123));
        //System.out.println("12344");
    }
}
